package cn.xspace.wnx.db;

import cn.xspace.chatui.data.IChatFileData;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by liuchen on 2016/10/14.
 */

public class ChatFileDao {

    private static ChatFileDao instance;

    public static final String TABLE_NAME = "t_file_record";
    /**
     * 插入时间
     */

    public static final String COLUMN_INSERTTIME = "inserttime";
    /**
     * 消息id
     */
    public static final String COLUMN_MSG_ID = "msg_id";
    /**
     * 文件名称
     */
    public static final String COLUMN_FILE_NAME = "file_name";
    /**
     * 文件本地路径
     */
    public static final String COLUMN_FILE_URL_LOCAL = "file_url_local";
    /**
     * 文件服务器路径
     */
    public static final String COLUMN_FILE_URL_REMOTE = "file_url_remote";
    /**
     * 文件大小
     */
    public static final String COLUMN_FILE_SIZE = "file_size";
    /**
     * 文件下载状态
     */
    public static final String COLUMN_FILE_DOWNLOAD_STATUS = "file_download_status";
    /**
     * 文件类型 1 图片 2语音  4视频  5.位置  7.文件
     */
    public static final String COLUMN_FILE_TYPE = "file_type";
    /**
     * 经度
     */
    public static final String COLUMN_LNG = "lng";
    /**
     * 纬度
     */
    public static final String COLUMN_LAT = "lat";
    /**
     * 语音时长
     */
    public static final String COLUMN_MSG_DUTIME = "msg_dutime";
    /**
     * 地址描述
     */
    public static final String COLUMN_ADDRESS_DESC = "address_desc";
    /**
     * 语音未读标志
     */
    public static final String COLUMN_IS_MEDIA_PLAYED = "isMediaPlayed";


    public static ChatFileDao getInstance() {
        if (instance == null) {
            instance = new ChatFileDao();
        }
        return instance;
    }

    public static final String FIlE_RECORD_TABLE_CREATE = "CREATE TABLE  IF NOT EXISTS "
            + ChatFileDao.TABLE_NAME
            + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
            + ChatFileDao.COLUMN_INSERTTIME + " TEXT,"
            + ChatFileDao.COLUMN_MSG_ID + " TEXT,"
            + ChatFileDao.COLUMN_FILE_NAME + " TEXT,"
            + ChatFileDao.COLUMN_FILE_URL_LOCAL + " TEXT,"
            + ChatFileDao.COLUMN_FILE_URL_REMOTE + " TEXT,"
            + ChatFileDao.COLUMN_FILE_SIZE + " TEXT,"
            + ChatFileDao.COLUMN_FILE_DOWNLOAD_STATUS + " INTEGER,"
            + ChatFileDao.COLUMN_FILE_TYPE + " INTEGER,"
            + ChatFileDao.COLUMN_LNG + " TEXT,"
            + ChatFileDao.COLUMN_LAT + " TEXT,"
            + ChatFileDao.COLUMN_MSG_DUTIME + " TEXT,"
            + ChatFileDao.COLUMN_IS_MEDIA_PLAYED + " INTEGER,"
            + ChatFileDao.COLUMN_ADDRESS_DESC + " TEXT)";

    private ChatFileDao() {
    }

    public IChatFileData getiChatFileData() {
        return iChatFileData;
    }

    IChatFileData iChatFileData = new IChatFileData() {
        @Override
        public Observable<String> updateFileDownLoadStausByTime(final String inserttime, final int status) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateFileDownLoadStausByTime(inserttime, status);
                    subscriber.onCompleted();
                }
            });
        }



        @Override
        public Observable<String> updateFileLocalPath(final String inserttime, final String path) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateFileLocalPathByTime(inserttime, path);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateFileMsgIdPath(final String time, final String msg_id) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateFileMsgIdByTime(time, msg_id);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateFileRemotePathByTime(final String time, final String path) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateFileRemotePathByTime(time, path);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateFileIsPlayedFlag(final String inserttime, final int flag) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateFileIsPlayedFlag(inserttime, flag);
                    subscriber.onCompleted();
                }
            });
        }
    };

    /**
     * 更新文件表的下载状态
     *
     * @param inserttime
     * @param status
     */
    public void updateFileDownLoadStausByTime(String inserttime, int status) {
        WNXDBManager.getInstance().updateFileDownLoadStausByTime(inserttime, status);
    }

    /**
     * 更新文件表的本地路径
     *
     * @param inserttime
     * @param path
     */
    public void updateFileLocalPath(String inserttime, String path) {
        WNXDBManager.getInstance().updateFileLocalPathByTime(inserttime, path);
    }

    /**
     * 更新文件的msg_id
     *
     * @param time
     * @param msg_id
     */
    public void updateFileMsgIdPath(String time, String msg_id) {
        WNXDBManager.getInstance().updateFileMsgIdByTime(time, msg_id);
    }

    /**
     * 更新文件的服务器路径
     *
     * @param time
     * @param path
     */
    public void updateFileRemotePathByTime(String time, String path) {
        WNXDBManager.getInstance().updateFileRemotePathByTime(time, path);
    }

    /**
     * 修改音频播放状态的标志
     *
     * @param inserttime
     * @param flag
     */
    public void updateFileIsPlayedFlag(String inserttime, int flag) {
        WNXDBManager.getInstance().updateFileIsPlayedFlag(inserttime, flag);
    }
}
